﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Setting_add_employee : SessionPage
{
	protected SYS_Employee empl = new SYS_Employee();
	protected bool ReadOnly = false;

	LogicLayer mgr = new LogicLayer();
	protected string tip = "";
	protected string curID = "", curUserName = "", curDeptID="", curDeptName="", curPositionName = "";

	protected void Page_Load(object sender, EventArgs e)
	{
		//检查权限
		this.VerifyFunction("setting");

		curID = Request.QueryString["id"] + "";
		if (!string.IsNullOrEmpty(curID))
		{
			empl = mgr.GetSYS_Employee(curID);
			curUserName = mgr.GetUserNameByEmplID(curID);			
			curDeptID = mgr.GetDeptIDByEmplID(curID);
			curDeptName = mgr.GetDeptNameByEmplID(curID);
			curPositionName = mgr.GetPositionNameByEmplID(curID);
		}
		if (empl == null)
			empl = new SYS_Employee();

		if (IsPostBack)
		{
			if (string.IsNullOrEmpty(curID))
			{
				SYS_Employee em = new SYS_Employee();
				em.EmplID = Guid.NewGuid().ToString();
				em.EmplName = Request.Form["emplName"];
				em.Sex = Request.Form["sex"];
				em.Birthday = Convert.ToDateTime(Request.Form["birth"]);
				em.Phone1 = Request.Form["tele1"];
				em.Mobile = Request.Form["mobile"];
				em.Email = Request.Form["email"];
				em.Messenger = Request.Form["pager"];

				SYS_User usr = new SYS_User();
				usr.UserName = Request.Form["username"];
				usr.Password = Request.Form["userpwd"];

				string isContinue = Request.Form["hidIsContinue"];

				bool result = true;
				if (mgr.AddEmployee(em, usr))
				{
					if (!string.IsNullOrEmpty(Request.Form["deptID"]))
					{
						SYS_EmplDept ed = new SYS_EmplDept();
						ed.DeptID = Request.Form["deptID"];
						ed.EmplID = em.EmplID;
						if (mgr.AddSYS_EmplDept(ed))
						{
							if (!string.IsNullOrEmpty(Request.Form["positionID"]))
							{
								SYS_EmplPosition ep = new SYS_EmplPosition();
								ep.EmplID = em.EmplID;
								ep.PositionID = Request.Form["positionID"];
								result &= mgr.AddSYS_EmplPosition(ep);
							}
						}
						else
							result = false;
					}
				}
				else
					result = false;

				if (result)
				{
					if (isContinue == "y")
					{
						tip = "保存成功!";
						Response.Write("<script>window.opener.location.reload();</script>");
					}
					else
						Response.Write("<script>window.opener.location.reload();window.close();</script>");
				}
				else
					tip = "保存失败!";
			}
			else
			{
				SYS_Employee em = mgr.GetSYS_Employee(curID);
				em.EmplName = Request.Form["emplName"];
				em.Sex = Request.Form["sex"];
				em.Birthday = Convert.ToDateTime(Request.Form["birth"]);
				em.Phone1 = Request.Form["tele1"];
				em.Mobile = Request.Form["mobile"];
				em.Email = Request.Form["email"];
				em.Messenger = Request.Form["pager"];

				string isContinue = Request.Form["hidIsContinue"];

				bool result = true;
				if (mgr.UpdateSYS_Employee(em))
				{
					if (!string.IsNullOrEmpty(Request.Form["deptID"]))
					{
						mgr.AddEmplDept(em.EmplID, Request.Form["deptID"],true);

						if (!string.IsNullOrEmpty(Request.Form["positionID"]))
							mgr.AddEmplPosition(em.EmplID,Request.Form["positionID"],true);
					}
				}
				else
					result = false;

				if (result)
				{
					if (isContinue == "y")
					{
						tip = "保存成功!";
						Response.Write("<script>window.opener.location.reload();</script>");
					}
					else
						Response.Write("<script>window.opener.location.reload();window.close();</script>");
				}
				else
					tip = "保存失败!";
			}
		}
	}

	protected void WritePosition()
	{
		List<SYS_Position> posList = mgr.ListSYS_Position();
		if (posList != null)
		{
			string sel = "";
			foreach (SYS_Position pos in posList)
			{
				sel = "";
				if (curPositionName == pos.Name)
					sel = " selected ";

				Response.Write("<option value=\"" + pos.PositionID + "\"" + sel + ">" + pos.Name + "</option>");
			}
		}
	}
}